package com.rocket.excel;

import java.io.File;
import java.util.ArrayList;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.rocket.entity.RocketRYI6300;

public class RocketRYI6300ExcelUtil extends ExcelUtil {

	private static RocketRYI6300ExcelUtil	mInstance;

	public static synchronized RocketRYI6300ExcelUtil getInstance() {
		if (mInstance == null) {
			mInstance = new RocketRYI6300ExcelUtil();
		}
		return mInstance;
	}

	public void export(final ArrayList<RocketRYI6300> rockets, final String path) {
		if (rockets == null || rockets.size() == 0) {
			return;
		}
		new Thread() {

			public void run() {

				File file = new File(path);

				try {
					WritableWorkbook wwb = Workbook.createWorkbook(file);
					WritableSheet sheet = wwb.createSheet("RYI-6300型火箭弹", 0);
					
					sheet.mergeCells(0, 0, 0, 1);
					sheet.mergeCells(1, 0, 2, 0);
					sheet.mergeCells(3, 0, 4, 0);
					sheet.mergeCells(5, 0, 6, 0);
					
					sheet.addCell(new Label(0, 0, "发射角（度）"));
					sheet.addCell(new Label(1, 0, "撒播起点"));
					sheet.addCell(new Label(1, 1, "射高（米）"));
					sheet.addCell(new Label(2, 1, "射程（米）"));
					sheet.addCell(new Label(3, 0, "最高点"));
					sheet.addCell(new Label(3, 1, "射高（米）"));
					sheet.addCell(new Label(4, 1, "射程（米）"));
					sheet.addCell(new Label(5, 0, "撒播终点"));
					sheet.addCell(new Label(5, 1, "射高（米）"));
					sheet.addCell(new Label(6, 1, "射程（米）"));
					sheet.addCell(new Label(7, 0, "理论落点"));
					sheet.addCell(new Label(7, 1, "射程（米）"));

					for (int i = 0; i < rockets.size(); i++) {
						RocketRYI6300 rocket = rockets.get(i);
						sheet.addCell(new Label(0, i + 2, String.valueOf(rocket.angle)));
						sheet.addCell(new Label(1, i + 2, String.valueOf(rocket.startHeight)));
						sheet.addCell(new Label(2, i + 2, String.valueOf(rocket.startLength)));
						sheet.addCell(new Label(3, i + 2, String.valueOf(rocket.topHeight)));
						sheet.addCell(new Label(4, i + 2, String.valueOf(rocket.topLength)));
						sheet.addCell(new Label(5, i + 2, String.valueOf(rocket.endHeight)));
						sheet.addCell(new Label(6, i + 2, String.valueOf(rocket.endLength)));
						sheet.addCell(new Label(7, i + 2, String.valueOf(rocket.length)));
					}

					// //作用是从(m,n)到(p,q)的单元格全部合并，比如：
					// WritableSheet sheet=wwb.createSheet("第一页",0);
					//
					// //合并第一列第一行到第六列第一行的所有单元格
					// sheet.mergeCells(0,0,5,0);

					wwb.write();
					wwb.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

			};
		}.start();
	}
}
